package com.example.mobile2021606127cxz0506;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edt;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Intent intent;
    private String input;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        edt = (EditText) findViewById(R.id.edt);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                input = edt.getText().toString().trim();
                if (input.length() == 0) {
                    Toast.makeText(this, "请输入网址", Toast.LENGTH_SHORT).show();
                } else {
                    uri = Uri.parse(input);
                    intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
                break;
            case R.id.btn2:
                input = edt.getText().toString().trim();
                if (input.length() == 0) {
                    Toast.makeText(this, "请输入号码", Toast.LENGTH_SHORT).show();
                } else {
                    uri = Uri.parse("tel:" + input);
                    intent = new Intent(Intent.ACTION_DIAL, uri);
                    startActivity(intent);
                }
                break;
            case R.id.btn3:
                intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivity(intent);
                break;
            case R.id.btn4:
                input = edt.getText().toString().trim();
                uri = Uri.parse("geo:" + input);
                intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                break;
        }
    }
}